package com.cristina.attention.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.cristina.attention.entity.Group;
import com.cristina.attention.entity.GroupMember;
import com.cristina.attention.entity.User;
import com.cristina.attention.entity.vo.GroupResVo;
import com.cristina.attention.entity.vo.GroupVo;
import com.cristina.attention.mapper.GroupMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author superman
 * @Date 2021/11/13 18:53
 **/
@Service
@Slf4j
@AllArgsConstructor
public class GroupServiceImpl extends ServiceImpl<GroupMapper, Group> implements GroupService {

    private final GroupMemberService groupMemberService;
    private final UserService userService;
    private final GroupMapper groupMapper;

    @Override
    public boolean create(GroupVo groupVo) {
        // 创建群
        Group group = Group.initGroup(groupVo.getUser().getUserId());
        group.setGroupName(groupVo.getGroupName());
        group.setGroupAvatar(groupVo.getUser().getImg());
        this.save(group);
        if (groupVo.getGroupMembers().size() < 1) {
            GroupMember groupMapper = new GroupMember();
            groupMapper.setUserId(group.getOwnerId());
            groupMapper.setGroupId(group.getGroupId());
            return groupMemberService.save(groupMapper);
        }
        // 保存 群-成员 关系
        return saveGroupMembers(groupVo, group.getGroupId());
    }

    @Override
    public boolean invite(GroupVo groupVo) {
        // 修改群头像, 群人数
        if (groupVo.getIsUpdateGroupAvatar()) {

        }
        // 邀请人入群, 初始化入群成员的群聊 Message 群容量暂且定为 10人,
        // 保存 群-成员 关系
        return saveGroupMembers(groupVo, groupVo.getGroupId());
    }

    private boolean saveGroupMembers(GroupVo groupVo, Long groupId) {
        List<User> members = userService.list(Wrappers.lambdaQuery(User.class).in(User::getUserId, groupVo.getGroupMembers()));
        List<GroupMember> groupMemberList = new ArrayList<>();
        for (User user : members) {
            GroupMember groupMapper = new GroupMember();
            groupMapper.setUserId(user.getUserId());
            groupMapper.setGroupId(groupId);
            groupMemberList.add(groupMapper);
        }
        return groupMemberService.saveBatch(groupMemberList);
    }

    @Override
    public boolean dissolution(Long groupId) {
        return SqlHelper.retBool(baseMapper.deleteById(groupId));
    }

    @Override
    public List<GroupResVo> findGroupBelonging(Long userId) {
        return getGroupResVos(userId);
    }

    private List<GroupResVo> getGroupResVos(Long userId) {
        List<Group> groupList = groupMapper.findGroupByUserId(userId);
        List<GroupResVo> list = new ArrayList<>();
        for (Group group : groupList) {
            GroupResVo groupResVo = new GroupResVo();
            groupResVo.setId(group.getId());
            groupResVo.setIndex(group.getIndex());
            GroupResVo.GroupInfo groupInfo = new GroupResVo.GroupInfo();
            groupInfo.setGroupName(group.getGroupName());
            groupInfo.setOwnerId(group.getOwnerId());
            groupInfo.setGroupId(group.getGroupId());
            groupInfo.setGroupAvatar(group.getGroupAvatar());
            groupResVo.setGroupInfo(groupInfo);
            list.add(groupResVo);
        }
        return list;
    }

}
